<?php
namespace app\api\controller\v1;

use \app\common\controller\BaseController;
use \app\common\model\User;

class Base extends BaseController
{
    /**
     * 登陆接口
     *
     * @return void
     */
    public function login()
    {
        $request = $this->request;
        $userModel = model("User");
        if (false === ($ret = $userModel->login($request->param('user_name'), $request->param('password')))) {
            return json_response([], STATUS_FAIL, $userModel->getError());
        } else {
            return json_response([$ret]);
        }
    
    }

    /**
     * 登出接口,不需要获取authKey
     *
     * @return void
     */
    public function logout(){
        User::clearToken($this->request->header('authkey'));
        return json_response([]);
    }

    /**
     * 获取当前用户的权限值，必须登录后才可以调用
     *
     * @return void
     */
    public function getPermissionLevel()
    {
        $authKey = $this->request->header('authkey');

        if (($ret= User::checkToken($authKey))!==STATUS_OK){
            return json_response([], $ret);
        }
        return json_response([cache(User::AUTHKEY_PREFIX.$authKey)['level']]);
    }

    /**
     * 获取当前服务器的时间戳,用于进行数据校验
     *
     * @return 时间戳
     */
    public function getTime(){
        return json_response([time()]);
    }

    




}
